<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入解析 Guava RateLimiter：优雅的限流实现</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            overflow-x: auto;
            position: relative;
        }
        
        .code-block::before {
            content: 'Java';
            position: absolute;
            top: 0;
            right: 0;
            background: #007acc;
            color: white;
            padding: 2px 12px;
            border-radius: 0 8px 0 8px;
            font-size: 12px;
            font-weight: 500;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
        }
        
        .feature-card {
            background: white;
            border-radius: 12px;
            padding: 24px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            transition: all 0.3s ease;
        }
        
        .feature-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
        }
        
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
        }
        
        table {
            border-collapse: separate;
            border-spacing: 0;
            border-radius: 8px;
            overflow: hidden;
        }
        
        th {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            font-weight: 600;
        }
        
        tr:nth-child(even) {
            background: #f8f9fa;
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl font-bold mb-6 leading-tight">
                    深入解析 Guava RateLimiter
                </h1>
                <p class="text-2xl mb-8 opacity-90">优雅的限流实现原理与实战</p>
                <div class="flex justify-center space-x-6 text-lg">
                    <span><i class="fas fa-clock mr-2"></i>阅读时间：15分钟</span>
                    <span><i class="fas fa-code mr-2"></i>源码解析</span>
                    <span><i class="fas fa-shield-alt mr-2"></i>高并发保护</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12 max-w-6xl">
        <!-- Introduction -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>高并发系统中，限流就像高速公路上的红绿灯，没它你可能短时间内跑得很快，但最终会因为拥堵而寸步难行。我用过不少限流框架，但Guava的RateLimiter凭借其简单而强大的API，成了我工作中的常客。今天，咱们就扒一扒它的内部实现。
            </p>
        </div>

        <!-- Section 1: 令牌桶算法实现原理 -->
        <section class="mb-12">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">1. 令牌桶算法实现原理</h2>
            
            <div class="grid md:grid-cols-2 gap-6 mb-8">
                <div class="feature-card">
                    <h3 class="text-xl font-semibold mb-4 text-purple-700">
                        <i class="fas fa-exclamation-triangle mr-2"></i>限流的必要性
                    </h3>
                    <ul class="space-y-3 text-gray-700">
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i><strong>防止系统过载</strong>：系统资源总是有限的</li>
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i><strong>防止连锁故障</strong>：上游限流避免下游雪崩</li>
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i><strong>业务削峰平谷</strong>：将突发流量分散到更长的时间处理</li>
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i><strong>应对恶意攻击</strong>：抵御DoS/DDoS攻击</li>
                    </ul>
                </div>
                
                <div class="feature-card">
                    <h3 class="text-xl font-semibold mb-4 text-purple-700">
                        <i class="fas fa-cogs mr-2"></i>限流算法概述
                    </h3>
                    <ol class="space-y-3 text-gray-700">
                        <li><span class="font-semibold text-purple-600">1. 计数器算法</span>：在固定时间窗口内限制请求数量</li>
                        <li><span class="font-semibold text-purple-600">2. 漏桶算法</span>：请求先进入漏桶，以固定速率流出</li>
                        <li><span class="font-semibold text-purple-600">3. 令牌桶算法</span>：以固定速率生成令牌，请求需要消耗令牌</li>
                    </ol>
                </div>
            </div>

            <div class="highlight-box">
                <p class="text-lg font-semibold mb-2">
                    <i class="fas fa-lightbulb mr-2"></i>血的教训
                </p>
                <p>我曾经维护过一个没有限流的API系统，正常情况下运行良好。但一次营销活动中，流量瞬间暴增10倍，数据库连接池迅速耗尽，连带影响了其他核心服务，最终整个平台都不可用了。</p>
                <p class="mt-2 font-bold">限流不是可选项，而是高并发系统的必选项。</p>
            </div>

            <!-- 令牌桶算法可视化 -->
            <div class="content-card rounded-xl shadow-lg p-6 mb-8">
                <h3 class="text-xl font-semibold mb-4 text-gray-800">
                    <i class="fas fa-chart-line mr-2"></i>令牌桶算法工作原理
                </h3>
                <div class="mermaid">
                    graph TD
                        A[令牌生成器] -->|固定速率| B[令牌桶]
                        B --> C{桶是否已满?}
                        C -->|是| D[丢弃多余令牌]
                        C -->|否| E[存储令牌]
                        F[请求到达] --> G{是否有令牌?}
                        G -->|是| H[消耗令牌<br/>处理请求]
                        G -->|否| I[等待或拒绝]
                        B --> G
                        style A fill:#667eea,stroke:#fff,color:#fff
                        style B fill:#764ba2,stroke:#fff,color:#fff
                        style H fill:#4ade80,stroke:#fff,color:#fff
                        style I fill:#f87171,stroke:#fff,color:#fff
                </div>
            </div>

            <!-- 基本用法代码示例 -->
            <div class="content-card rounded-xl shadow-lg p-6">
                <h3 class="text-xl font-semibold mb-4 text-gray-800">
                    <i class="fas fa-code mr-2"></i>RateLimiter的基本用法
                </h3>
                <pre class="code-block p-4"><code>// 创建限流器，每秒允许5个请求
RateLimiter limiter = RateLimiter.create(5.0);

// 阻塞直到获取到令牌
limiter.acquire(); // 获取1个令牌
limiter.acquire(4); // 获取4个令牌

// 非阻塞方式，尝试获取令牌
boolean acquired = limiter.tryAcquire(1, 100, TimeUnit.MILLISECONDS);</code></pre>
                <p class="mt-4 text-gray-600">使用起来就是这么简单，但内部实现却很有深度。</p>
            </div>
        </section>

        <!-- Section 2: 平滑限流策略源码分析 -->
        <section class="mb-12">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">2. 平滑限流策略源码分析</h2>
            
            <div class="grid md:grid-cols-2 gap-6 mb-8">
                <div class="content-card rounded-xl shadow-lg p-6">
                    <h3 class="text-xl font-semibold mb-4 text-purple-700">
                        <i class="fas fa-rocket mr-2"></i>SmoothBursty
                    </h3>
                    <p class="text-gray-700 mb-3">允许一定程度的突发流量，适用于大多数场景。</p>
                    <ul class="space-y-2 text-gray-600">
                        <li><i class="fas fa-bolt text-yellow-500 mr-2"></i>瞬间消耗所有存储令牌</li>
                        <li><i class="fas fa-tachometer-alt text-blue-500 mr-2"></i>应对突发流量</li>
                        <li><i class="fas fa-balance-scale text-green-500 mr-2"></i>长期平均速率受控</li>
                    </ul>
                </div>
                
                <div class="content-card rounded-xl shadow-lg p-6">
                    <h3 class="text-xl font-semibold mb-4 text-purple-700">
                        <i class="fas fa-fire mr-2"></i>SmoothWarmingUp
                    </h3>
                    <p class="text-gray-700 mb-3">带有预热期的平滑限流器，适用于冷启动场景。</p>
                    <ul class="space-y-2 text-gray-600">
                        <li><i class="fas fa-thermometer-half text-orange-500 mr-2"></i>逐渐提升处理速率</li>
                        <li><i class="fas fa-shield-alt text-purple-500 mr-2"></i>保护冷启动系统</li>
                        <li><i class="fas fa-chart-line text-indigo-500 mr-2"></i>平滑的速率曲线</li>
                    </ul>
                </div>
            </div>

            <!-- 核心源码解析 -->
            <div class="content-card rounded-xl shadow-lg p-6 mb-8">
                <h3 class="text-xl font-semibold mb-4 text-gray-800">
                    <i class="fas fa-microscope mr-2"></i>SmoothRateLimiter核心实现
                </h3>
                <pre class="code-block p-4 mb-4"><code>abstract class Smo